<template>
  <ScrollWrapper>
    <div class="container">
      <div class="container-content">
        <!-- 页面头部 -->
        <div class="header">
          <h2 class="title"></h2>
          <div class="actions">
            <a-button :loading="syncLoading" class="sync-btn" @click="handleSync">同步</a-button>
            <a-button type="primary" class="add-btn" @click="handleAdd">添加</a-button>
          </div>
        </div>

        <!-- 数据表格 -->
        <div class="table-container pagination-container">
          <a-table
              :columns="columns"
              :data-source="dataSource"
              :scroll="{ x: 1200 }"
              class="department-table"
          >
            <template #bodyCell="{ column, record }">
              <template v-if="column.key === 'action'">
                <div class="action-buttons">
                  <a-button size="small" class="detail-btn" @click="handleViewDetail(record)">
                    查看详情
                  </a-button>
                  <a-button
                      size="small"
                      :class="record.status === '正常' ? 'disable-btn' : 'enable-btn'"
                      @click="handleToggleStatus(record)"
                  >
                    {{ record.status === '正常' ? '作废' : '恢复' }}
                  </a-button>
                </div>
              </template>
            </template>
          </a-table>
        </div>
      </div>
    </div>
  </ScrollWrapper>
</template>

<script>
import ScrollWrapper from "@/components/ScrollWrapper/ScrollWrapper.vue";
import {message} from 'ant-design-vue';
import help from "@/utils/help.js";
import api from "@/api/index.js";

export default {
  components: {
    ScrollWrapper,
  },
  data() {
    return {
      syncLoading: false,
      currentPage: 1,
      pageSize: 10,
      total: 40,
      columns: [
        {
          title: '部门ID',
          dataIndex: 'departmentId',
          key: 'departmentId',
          align: 'center'
        },
        {
          title: '部门名称',
          dataIndex: 'departmentName',
          key: 'departmentName',
          align: 'center'
        },
        {
          title: '部门负责人',
          dataIndex: 'leader',
          key: 'leader',
          align: 'center'
        },
        {
          title: '数据状态',
          dataIndex: 'status',
          key: 'status',
          align: 'center'
        },
        {
          title: '同步时间',
          dataIndex: 'syncTime',
          key: 'syncTime',
          align: 'center'
        },
        {
          title: '',
          key: 'action',
          align: 'center'
        }
      ],
      dataSource: []
    }
  },
  mounted() {
    this.getList();
  },
  methods: {
    async getList() {
      try {
        let reuslt = await api.getDepartment({
          isSubject: false
        })
        console.log("reuslt", reuslt)
        this.dataSource = reuslt.map((item) => {
          item.status = '正常'
          return item;
        });
      } catch (error) {
        help.error(error)
      }
    },
    async handleSync() {
      try {
        this.syncLoading = true;
        let reuslt = await api.syncDepartment({})
        message.success("部门信息同步成功")
      } catch (error) {
        help.error(error)
      } finally {
        this.syncLoading = false;
      }
    },
    handleAdd() {
      message.info('无添加部门接口,是否只保留同步的信息');
    },
    handleViewDetail(record) {
      message.info(`查看 ${record.departmentName} 详情`);
    },
    handleToggleStatus(record) {
      message.info('无相关接口,是否删除该功能');
      const newStatus = record.status === '正常' ? '作废' : '正常';
      record.status = newStatus;
      // message.success(`${record.departmentName} 已${newStatus}`);
    },
  }
}
</script>

<style scoped lang="scss">
.container {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  padding: 0px 20px 24px 0;
  box-sizing: border-box;
}

.container-content {
  background-color: #fff;
  display: flex;
  margin-top: 20px;
  flex-direction: column;
  border-radius: 30px;
  flex: 1;
  padding: 24px 0;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;

  .title {
    font-size: 24px;
    font-weight: 600;
    color: #333;
    margin: 0;
  }

  .actions {
    display: flex;
    gap: 12px;
    margin: 0 30px;

    .sync-btn {
      color: #3a7c5a;
      font-weight: 400;
      border-radius: 6px;
      width: 110px;
      border-color: #3a7c5a;

      &:hover {
        opacity: 0.6;
      }
    }

    .add-btn {
      background-color: #3a7c5a;
      border-radius: 6px;
      width: 110px;

      &:hover {
        opacity: 0.6;
      }
    }
  }
}

.table-container {
  flex: 1;
  margin-bottom: 24px;

  :deep(.department-table) {
    .ant-table-thead > tr > th {
      background-color: #fff;
      font-weight: 500;
      color: #666;
      border-bottom: 1px solid #f0f0f0;
      padding: 16px 8px;
    }

    .ant-table-tbody > tr > td {
      padding: 16px 8px;
      border-bottom: 1px solid #f0f0f0;
      color: #333;
    }

    .ant-table-tbody > tr:hover > td {
      background-color: #f5f5f5;
    }

    .ant-table-cell:before {
      width: 0 !important;
    }

    .ant-table-thead > tr > .ant-table-cell {
      color: #d9d9d9;
    }
  }

  .action-buttons {
    display: flex;
    gap: 8px;
    justify-content: center;

    .detail-btn {
      background-color: #fff;
      border: 1px solid #3a7c5a;
      color: #3a7c5a;
      border-radius: 4px;
      font-size: 12px;
      width: 80px;

      &:hover {
        opacity: 0.6;
      }
    }

    .enable-btn {
      background-color: #3a7c5a;
      color: #fff;
      border-radius: 4px;
      font-size: 12px;
      width: 80px;

      &:hover {
        opacity: 0.6;
      }
    }

    .disable-btn {
      background-color: #ff4d4f;
      border-color: #ff4d4f;
      color: #fff;
      border-radius: 4px;
      font-size: 12px;
      width: 80px;

      &:hover {
        background-color: #ff7875;
        border-color: #ff7875;
      }
    }
  }
}

.pagination-container {
  //display: flex;
  //justify-content: flex-end;
  //padding-top: 16px;
  //border-top: 1px solid #f0f0f0;

  :deep(.ant-pagination) {
    .ant-pagination-item {
      border-radius: 4px;

      &.ant-pagination-item-active {
        background-color: #3a7c5a;
        border-color: #3a7c5a;

        a {
          color: #fff;
        }
      }
    }

    .ant-pagination-prev,
    .ant-pagination-next {
      border-radius: 4px;

      &:hover {
        border-color: #3a7c5a;
        color: #3a7c5a;
      }
    }
  }
}

:deep(.ant-tag) {
  border-radius: 12px;
  padding: 2px 8px;
  font-size: 12px;

  &.ant-tag-green {
    background-color: #f6ffed;
    border-color: #b7eb8f;
    color: #52c41a;
  }

  &.ant-tag-orange {
    background-color: #fff7e6;
    border-color: #ffd591;
    color: #fa8c16;
  }
}
</style>